import User from "@/pojo/User";

class LoginController {
  constructor() {
    const token = localStorage.getItem("userToken");
    if (token !== null) {
      this.hasLogin = true;
      this.userInfo = new User().setToken(Number(token)).setName("用户").setIcon("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
      this.remenberMe = true;
    } else {
      this.hasLogin = false;
      this.userInfo = new User();
      this.remenberMe = false;
    }
  }

  /**
   * Return whether user has logon
   * @return {boolean} logon statement
   */
  getHasLogin() {
    return this.hasLogin;
  }

  /**
   * Return user info
   * @return {User} user info
   */
  getUserInfo() {
    return this.userInfo;
  }

  /**
   * Login by user info
   * @param {User} user user info
   */
  login(user) {
    this.userInfo = user;
    if (this.remenberMe) {
      localStorage.setItem("userToken", user.getToken().toString());
    }
  }
}

const loginController = new LoginController();
export default loginController;